package vip.denglei.company.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import vip.denglei.company.mapper.WorkWechatMsgContextMapper;
import vip.denglei.company.mapper.WorkWechatMsgInfoMapper;
import vip.denglei.company.mapper.WorkWechatTaskManageMapper;
import vip.denglei.company.mapper.WorkWechatUserInfoMapper;
import vip.denglei.company.model.WorkWechatTaskManage;
import vip.denglei.company.util.DateUtils;
import vip.denglei.company.util.HttpResult;
import vip.denglei.company.util.WorkWechatSendMsgUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

@RestController
public class TaskController {
    protected static Logger logger= LoggerFactory.getLogger(TaskController.class);
    @Autowired
    private WorkWechatTaskManageMapper workWechatTaskManageMapper;
    @Autowired
    private WorkWechatUserInfoMapper workWechatUserInfoMapper;
    @Autowired
    public WorkWechatMsgContextMapper workWechatMsgContextMapper;
    @Autowired
    public WorkWechatMsgInfoMapper workWechatMsgInfoMapper;

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public HttpResult getUserInfo() {

        return HttpResult.success(workWechatUserInfoMapper.getUserInfoList());
    }
    @RequestMapping(value = "/submiTask", method = RequestMethod.POST)
    public HttpResult submiTask(@RequestBody HashMap map) throws ParseException {
        WorkWechatTaskManage workWechatTaskManage=new WorkWechatTaskManage();
        workWechatTaskManage.setTaskName(map.get("taslName").toString());
        workWechatTaskManage.setTaskMsg(map.get("taskMsg").toString());
        workWechatTaskManage.setTaskWorker(map.get("taskTaskWorker").toString());
        workWechatTaskManage.setTaskWorkerId(map.get("taskWorkId").toString());
        workWechatTaskManage.setStartTime(DateUtils.unixTimeFormat(map.get("startTime").toString()));
        workWechatTaskManage.setEndTime(DateUtils.unixTimeFormat(map.get("endTime").toString()));
        workWechatTaskManage.setExpandTime(DateUtils.unixTimeFormat(map.get("endTime").toString()));
        workWechatTaskManage.setCreateTime(new Date());
        workWechatTaskManageMapper.insert(workWechatTaskManage);
        WorkWechatSendMsgUtils.sendMsg(workWechatTaskManage.getTaskWorkerId(),"新建任务【"+workWechatTaskManage.getTaskName()+"】需要你的协助，请孰知！");
        WorkWechatSendMsgUtils.setBotMsg("请知晓，新增任务【"+workWechatTaskManage.getTaskName()+"】,负责人："+workWechatTaskManage.getTaskWorker(),1,3,workWechatMsgInfoMapper,workWechatMsgContextMapper);
        return HttpResult.success("任务新建成功");
    }
    @RequestMapping(value = "/getTaskList", method = RequestMethod.GET)
    public HttpResult getTaskList(){
        return HttpResult.success(workWechatTaskManageMapper.getTaskList());
    }

    /**
     * 任务延期
     * @param map
     * @return
     */
    @RequestMapping(value = "/expandTask", method = RequestMethod.POST)
    public HttpResult expandTask(@RequestBody HashMap map) throws ParseException {
       WorkWechatTaskManage workWechatTaskManage= workWechatTaskManageMapper.selectByPrimaryKey(Integer.parseInt(map.get("id").toString()));
       workWechatTaskManage.setExpandTime(DateUtils.unixTimeFormat(map.get("expandTime").toString()));
        workWechatTaskManageMapper.updateByPrimaryKey(workWechatTaskManage);
        WorkWechatSendMsgUtils.sendMsg(workWechatTaskManage.getTaskWorkerId(),"任务【"+workWechatTaskManage.getTaskName()+"】延期时间为："+map.get("expandTime").toString()+"，请好好掌控时间！");
        WorkWechatSendMsgUtils.setBotMsg("任务【"+workWechatTaskManage.getTaskName()+"】延期时间为："+map.get("expandTime").toString()+",负责人："+workWechatTaskManage.getTaskWorker()+"，请各位及时跟踪监查确保延期时间内完成任务！",1,3,workWechatMsgInfoMapper,workWechatMsgContextMapper);
       return HttpResult.success("任务延期成功");
    }
    /**
     * 任务完成
     * @param map
     * @return
     */
    @RequestMapping(value = "/completeTask", method = RequestMethod.POST)
    public HttpResult completeTask(@RequestBody HashMap map) throws ParseException {
        WorkWechatTaskManage workWechatTaskManage= workWechatTaskManageMapper.selectByPrimaryKey(Integer.parseInt(map.get("id").toString()));
        workWechatTaskManage.setCompleteTime(DateUtils.unixTimeFormat(map.get("completeTime").toString()));
        workWechatTaskManageMapper.updateByPrimaryKey(workWechatTaskManage);
        WorkWechatSendMsgUtils.sendMsg(workWechatTaskManage.getTaskWorkerId(),"你的任务【"+workWechatTaskManage.getTaskName()+"】已经完成，请继续加油！");
        WorkWechatSendMsgUtils.setBotMsg("已完成任务【"+workWechatTaskManage.getTaskName()+"】,负责人："+workWechatTaskManage.getTaskWorker(),1,3,workWechatMsgInfoMapper,workWechatMsgContextMapper);
        return HttpResult.success("任务已完成");
    }

    /**
     * 提醒任务
     * @param map
     * @return
     */
    @RequestMapping(value = "/remindTask", method = RequestMethod.POST)
    public HttpResult remindTask(@RequestBody HashMap map){
        WorkWechatTaskManage workWechatTaskManage= workWechatTaskManageMapper.selectByPrimaryKey(Integer.parseInt(map.get("id").toString()));
        WorkWechatSendMsgUtils.sendMsg(workWechatTaskManage.getTaskWorkerId(),"你的任务【"+workWechatTaskManage.getTaskName()+"】请注意结束时间，如遇到困难可联系邓磊协助！");
        return HttpResult.success("已完成提醒");
    }



}
